package com.dinadi.yidi.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

import com.dinadi.yidi.R;


/**
 * 选择图片的方式，拍照，图片
 * @author nolan
 *
 */
public class SelectImage extends PopupWindow implements OnClickListener {

	private Activity mContext;
	private View mMenuView;
	private ViewFlipper viewfipper;
	private RelativeLayout btn_Camera, btn_photo, btn_cancel;

	public SelectImage(Activity context) {
		super(context);
		mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//多加这一句，问题就解决了！这句的官方文档解释是：让窗口背景后面的任何东西变暗  
		mMenuView = inflater.inflate(R.layout.dialog_photo, null);
		viewfipper = new ViewFlipper(context);
		viewfipper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		btn_Camera = (RelativeLayout) mMenuView.findViewById(R.id.popup_selece_image_camera);
		btn_photo = (RelativeLayout) mMenuView.findViewById(R.id.popup_selece_image_photo);
		btn_cancel = (RelativeLayout) mMenuView.findViewById(R.id.popup_selece_image_cancel);

		btn_Camera.setOnClickListener(this);
		btn_photo.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
		
		viewfipper.addView(mMenuView);
		viewfipper.setFlipInterval(6000000);
		this.setContentView(viewfipper);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x20000000);
		this.setBackgroundDrawable(dw);
		this.update();

	}
	
	private OnClickListener listener_photo, listener_camera;
	
	public void setCameraListener(OnClickListener listener){
        this.listener_camera = listener;
	}
	
	public void setPhotoListener(OnClickListener listener){
        this.listener_photo = listener;
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		viewfipper.startFlipping();
		WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
		lp.alpha = 0.3f;
		mContext.getWindow().setAttributes(lp);

		setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
				lp.alpha = 1f;
				mContext.getWindow().setAttributes(lp);
			}
		});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.popup_selece_image_camera:
			if(listener_camera != null)
				listener_camera.onClick(btn_Camera);
			break;
		case R.id.popup_selece_image_photo:
			if(listener_photo != null)
				listener_photo.onClick(btn_photo);
			break;

		default:
			break;
		}
		this.dismiss();
	}
	


}
